package km.TestViewSwitcher;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class TestViewSwitcher extends ListActivity	 implements OnClickListener {

//sample list items
static final String[] ITEMS = new String[]
{ "List Item 1", "List Item 2", 
"List Item 3", "List Item 4", 
"List Item 5", "List Item 6", 
"List Item 7", "List Item 8", 
"List Item 9", "List Item 10" };

//the ViewSwitcher
private ViewSwitcher switcher;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//no window title
requestWindowFeature(Window.FEATURE_NO_TITLE);

//create the ViewSwitcher in the current context
switcher = new ViewSwitcher(this);

//footer Button: see XML1
Button footer = (Button)View.inflate(this, R.layout.btn_loadmore, null);

//progress View: see XML2
View progress = View.inflate(this, R.layout.loading_footer, null);

//add the views (first added will show first)
switcher.addView(footer);
switcher.addView(progress);

//add the ViewSwitcher to the footer
getListView().addFooterView(switcher);

//add items to the ListView
setListAdapter(new ArrayAdapter(this,
 android.R.layout.simple_list_item_1, ITEMS));
}

public void onClick(View arg0) {
//first view is showing, show the second progress view
switcher.showNext();
//and start background work
new getMoreItems().execute();
}

/** Background Task To Get More Items**/
private class getMoreItems extends AsyncTask {
	protected Object doInBackground(Object... params) {
	//code to add more items
	//...
	try {
		Thread.sleep(3000); //only to demonstrate
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	return null;
	}

@Override /* Background Task is Done */
protected void onPostExecute(Object result) {
//go back to the first view
switcher.showPrevious();
           //update the ListView
}
}
}
